﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion.Internal;
using Models.Common;
using Models.Device;
using Models.Task;
using Nancy.TinyIoc;
using Newtonsoft.Json;

using GatewayServerService.Device;

using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Data;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using static Models.Common.EnumDeviceState;
using Dm;
using UtilsBase;
using UtilsORM.MySqlData;
using UtilsORM.MySqlData;
using UtilsORM;
using UtilsCache;

namespace GatewayServerService.Data
{
    /// <summary>
    /// 计划任务、流程步骤执行
    /// </summary>
    public class DBIotDeviceTaskPlainLog
    {

        static string LogSave = UtilAppsettingCache.getSetting("base:logSave");
        public async void addTaskPlianTaskLog(string tpguid, int status, string remark)
        {

            try
            {

                if (LogSave == "false")
                {
                    return;
                }
                // EFYihongIotContext efdc = new EFYihongIotContext();
                EFYihongIotContext ef = new EFYihongIotContext();
                iot_device_task_plain plain = ef.iot_device_task_plain.Where(c => c.tp_guid == tpguid).FirstOrDefault();

                iot_device_task_plain_log plain_Log = new iot_device_task_plain_log();
                plain_Log.l_guid = Guid.NewGuid().ToString();
                plain_Log.l_title = plain.tp_title;
                plain_Log.l_tp_guid = plain.tp_guid;// tpguid;
                plain_Log.l_add_time = DateTime.Now;
                plain_Log.l_status = status;
                plain_Log.l_remark = remark;
                plain_Log.l_execute_batch_no = plain.tp_execute_batch_no;
                await ef.AddAsync(plain_Log);
                ef.SaveChanges();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
        public async void addTaskPlianTaskLog(iot_device_task_plain plain, int status, string remark)
        {
            try
            {

                if (LogSave == "false")
                {
                    return;
                }
                // iot_device_task_plain plain = ef.iot_device_task_plain.Where(c => c.tp_guid == tpguid).FirstOrDefault();
                // EFYihongIotContext efdc = new EFYihongIotContext();
                EFYihongIotContext ef = new EFYihongIotContext();

                iot_device_task_plain_log plain_Log = new iot_device_task_plain_log();
                plain_Log.l_guid = Guid.NewGuid().ToString();
                plain_Log.l_title = plain.tp_title;
                plain_Log.l_tp_guid = plain.tp_guid;// tpguid;
                plain_Log.l_add_time = DateTime.Now;
                plain_Log.l_status = status;
                plain_Log.l_remark = remark;
                plain_Log.l_execute_batch_no = plain.tp_execute_batch_no;
                await ef.AddAsync(plain_Log);
                ef.SaveChanges();


                //dc_iot_device_task_plain_log dclog = new UtilsClassClone().CloneByReflection<iot_device_task_plain_log, dc_iot_device_task_plain_log>(plain_Log);
                //efdc.dc_iot_device_task_plain_log.AddAsync(dclog);
                //efdc.SaveChanges();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

        public async void addTaskPlianTaskLogItem(iot_device_task_plain_item plain_Item, int status, string remark)
        {
            try
            {
                //iot_device_task_plain_item plain_Item = ef.iot_device_task_plain_item.Where(c => c.tpi_guid == tpiguid).FirstOrDefault();
                if (LogSave == "false")
                {
                    return;
                }


                //EFYihongIotContext efdc = new EFYihongIotContext();
                EFYihongIotContext ef = new EFYihongIotContext();
                iot_device_task_plain_item_log item_Log = new iot_device_task_plain_item_log();
                item_Log.li_guid = Guid.NewGuid().ToString();
                item_Log.li_title = plain_Item.tpi_title;
                item_Log.li_tpi_guid = plain_Item.tpi_guid;// tpiguid;
                item_Log.li_add_time = DateTime.Now;
                item_Log.li_status = status;
                item_Log.li_remark = remark;
                item_Log.li_tp_guid = plain_Item.tpi_tp_guid;
                item_Log.li_execute_batch_no = plain_Item.tpi_execute_batch_no;
                await ef.AddAsync(item_Log);
                ef.SaveChanges();


                //dc_iot_device_task_plain_item_log dclog = new UtilsClassClone().CloneByReflection<iot_device_task_plain_item_log, dc_iot_device_task_plain_item_log>(item_Log);
                //efdc.dc_iot_device_task_plain_item_log.AddAsync(dclog);
                //efdc.SaveChanges();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

        public void addTaskPlianTaskLogItem(iot_device_task_plain_item_handle plain_Item, int status, string remark)
        {
            try
            {

                if (LogSave == "false")
                {
                    return;
                }



                // EFYihongIotContext efdc = new EFYihongIotContext();
                EFYihongIotContext ef = new EFYihongIotContext();

                Console.WriteLine("流程：" + plain_Item.tpi_title + "->处理结果：" + remark);
                iot_device_task_plain_item_log item_Log = new iot_device_task_plain_item_log();
                item_Log.li_guid = Guid.NewGuid().ToString();
                item_Log.li_title = plain_Item.tpi_title;
                item_Log.li_tpi_guid = plain_Item.tpi_guid;// tpiguid;
                item_Log.li_add_time = DateTime.Now;
                item_Log.li_status = status;
                item_Log.li_remark = remark;
                item_Log.li_tp_guid = plain_Item.tpi_tp_guid;
                item_Log.li_execute_batch_no = plain_Item.tpi_execute_batch_no;
                ef.Add(item_Log);
                ef.SaveChanges();

                // ef = new EFIotGatewayContext();
                // ef.iot_device_task_plain_item_handle.Remove(plain_Item);
                // ef.SaveChanges();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
        /// <summary>
        /// //删除待办
        /// </summary>
        /// <param name="plain_Item"></param>
        public void delHandle(iot_device_task_plain_item_handle plain_Item)
        {
            try
            {

                EFYihongIotContext ef = new EFYihongIotContext();
                ef.iot_device_task_plain_item_handle.Remove(plain_Item);
                ef.SaveChanges();
                ef.Dispose();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

    }
}
